package edu.pku.sei.transformation.editor.controller.commands.link;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;

public class LinkDeleteCommand extends AbstractLinkDeleteCommand<MetaLink, MetaNode, MetaNode> {
	private Command modelCmd;

	public LinkDeleteCommand(MetaLink link) {
		this.link = link;
		this.source = link.getSource();
		this.target = link.getTarget();
	}

	public Command getModelCmd() {
		return modelCmd;
	}

	public void setModelCmd(Command modelCmd) {
		this.modelCmd = modelCmd;
		this.setLabel(modelCmd.getLabel());
	}

	@Override
	public boolean canExecute() {
		boolean modelState = (modelCmd != null) ? modelCmd.canExecute() : true;

		if (this.link != null && this.source != null && this.target != null) {
			return modelState;
		} else {
			return false;
		}
	}

	@Override
	public void execute() {
		this.source.getOutgoings().remove(link);
		this.target.getIncomings().remove(link);
		if (modelCmd != null) {
			modelCmd.execute();
		}
	}

	@Override
	public void undo() {
		this.source.getOutgoings().add(link);
		this.target.getIncomings().add(link);
		if (modelCmd != null) {
			modelCmd.undo();
		}
	}
}
